<?php
namespace app\admin\controller;
class ProductController extends Basic
{
    public function index(){
       
    }
    
    public function products(){
        $this->menu(0,58);
        $model =  \think\Db::table('product')->select();
        $this->assign('list',$model);
        return $this->fetch();
    }        
    
    public function addProducts(){
        $this->menu(58,60);
        return $this->fetch();
    }


    public function updateProduct(){
        $this->menu(58,59);
        $id = input('id','','trim');
        $data = \think\Db::table('product')->where('id',$id)->find();
        $pics = \think\Db::table('picture')->where('productId',$id)->select();
        $this->assign('data',$data);
        $this->assign('Pics',$pics);
        return $this->fetch();
    }
    
   public function delProduct(){
         $id = \think\Request::instance()->get('id');
         $ret = \think\Db::table('product')->delete($id);
         if($ret>0){
            return  $this->success('删除成功','ProductController/products');
         }else{
            return $this->error('错误');
         }
    }
    
    public function saveProduct(){
        if(isset($_POST['delPic'])){
            $delPhoto = $_POST['delPic'];
            foreach($delPhoto as $v){
                if(strlen($v)>1){
                    $this->delPhoto($v);
                }
            }
        }
        
        
       $price = input('price','','trim');
       $id = input('id','','trim');
       $name = input('name','','trim');
       $detail = input('detail','','trim');
       $mainPic = input('mainPic','','trim');
       
        $arr = array(
            'table'=>'product',
            'id'=>$id,
            'price'=>$price,
            'name'=>$name,
            'detail'=>$detail,
            'mainPicture'=>$mainPic,
            'edittime'=>date('Y-m-d H:i:s')
        );
        
        $crud = \think\Loader::model('Crud','logic');
        $ret = $crud->SaveData($arr);
        
        if($ret){
            if(isset($_POST["detailPic"])){
                 $detailPic= $_POST["detailPic"];
                 $productId = $ret['id'];
                 $crud->delPic($productId);
                 
                  foreach($detailPic as $v){
                    $url = $v;

                    $Parr = array(
                      'table'=>'picture', 
                      'id'=>"",
                      'productId'=>$productId,
                      'url'=>$url,
                    );

                    $crud->SaveData($Parr);
                }
            }
           return $this->success('成功','ProductController/products');
        }else{
            return $this->error('出错了');
        }
    }
    
    public function savePic(){
        $typeArr = array("jpg", "png", "gif");//允许上传文件格式 
        $path= ROOT_PATH.'public\uploads\product';

        if (isset($_POST)) { 
        $name = $_FILES['file']['name']; 
        $size = $_FILES['file']['size']; 
        $name_tmp = $_FILES['file']['tmp_name']; 
        if (empty($name)) { 
            echo json_encode(array("error"=>"您还未选择图片")); 
            exit; 
        } 
        $type = strtolower(substr(strrchr($name, '.'), 1)); //获取文件类型 

        if (!in_array($type, $typeArr)) { 
            echo json_encode(array("error"=>"清上传jpg,png或gif类型的图片！")); 
            exit; 
        } 
        if ($size > (3072 * 1024)) { 
            echo json_encode(array("error"=>"图片大小已超过3m！")); 
            exit; 
        } 

        $pic_name = time() . rand(10000, 99999) . "." . $type;//图片名称 
        $pic_url ="$path\\$pic_name";//上传后图片路径+名称 
        if (move_uploaded_file($name_tmp, $pic_url)) { //临时文件转移到目标文件夹 
            return json_encode(array("error"=>"0","pic"=>"/uploads/product/$pic_name","name"=>$pic_name)); 
        } else { 
            return json_encode(array("error"=>"上传有误，清检查服务器配置！")); 
        } 
        }
    }
}